package com.bsg.upm.check;

import java.text.MessageFormat;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.bsg.upm.constant.Messages;
import com.bsg.upm.entity.UserEntity;
import com.bsg.upm.util.EncryptionUtils;

/**
 * 用户注册业务处理检查
 * 
 * @author HCK
 *
 */
@Service
public class UserCheck extends BaseCheck {

	/**
	 * 用户注册时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkSave(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkSaveNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkSaveLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 用户编辑时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdate(Map<String, Object> paramMap) {
		// 非逻辑检查
		CheckResult chkRS = checkUpdateNonLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}

		// 逻辑检查
		chkRS = checkUpdateLogic(paramMap);
		if (chkRS.isError()) {
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 密码变更时检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkUpdatePwd(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 用户名非空检查
		String username = (String) paramMap.get("username");
		if (StringUtils.isBlank(username)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户名"));
			return chkRS;
		}

		// 密码非空和有效性检查
		String oldPassword = (String) paramMap.get("oldPassword");
		if (StringUtils.isBlank(oldPassword)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "原密码"));
			return chkRS;
		}
		String newPassword = (String) paramMap.get("newPassword");
		if (StringUtils.isBlank(newPassword)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "新密码"));
			return chkRS;
		}
		if (oldPassword.equals(newPassword)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("原密码和新密码不能为相同");
			return chkRS;
		}

		// 用户存在性检查
		UserEntity user = userDao.getUser(username, null);
		if (user == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "用户"));
			return chkRS;
		}

		// 非平台用户不能更改密码
		if (!user.getAuthType().equals("native")) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("非平台用户不能更改密码");
			return chkRS;
		}

		user = userDao.getUser(username, EncryptionUtils.getMD5(oldPassword));
		if (user == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("原密码错误");
			return chkRS;
		}

		if (user.getEnabled() == false) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("用户已被停用");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 密码重置时检查
	 * 
	 * @param user
	 *            用户对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkResetPwd(UserEntity user) {
		CheckResult chkRS = new CheckResult();
		// 用户存在性检查
		if (user == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "用户"));
			return chkRS;
		}

		if (!user.getAuthType().equals("native")) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("非平台用户不能重置密码");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 用户启用时检查
	 * 
	 * @param user
	 *            用户对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkEnable(UserEntity user) {
		CheckResult chkRS = new CheckResult();
		// 用户存在性检查
		if (user == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "用户"));
			return chkRS;
		}

		// 用户状态检查
		if (user.getEnabled()) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(Messages.ENABLED_ALREADY);
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 用户停用时检查
	 * 
	 * @param user
	 *            用户对象
	 * @return CheckResult检查结果对象
	 */
	public CheckResult checkDisable(UserEntity user) {
		CheckResult chkRS = new CheckResult();
		// 用户存在性检查
		if (user == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "用户"));
			return chkRS;
		}

		// 用户状态检查
		if (!user.getEnabled()) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg(Messages.DISABLED_ALREADY);
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 注册时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 用户名非空和长度检查
		String username = (String) paramMap.get("username");
		if (StringUtils.isBlank(username)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户名"));
			return chkRS;
		}
		if (StringUtils.trim(username).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "用户名", 32));
			return chkRS;
		}

		// 密码非空检查
		String password = (String) paramMap.get("password");
		if (StringUtils.isBlank(password)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "密码"));
			return chkRS;
		}

		// 用户姓名非空和长度检查
		String name = (String) paramMap.get("name");
		if (StringUtils.isBlank(name)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户姓名"));
			return chkRS;
		}
		if (StringUtils.trim(name).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "用户姓名", 32));
			return chkRS;
		}

		// 手机号非空和有效性检查
		String telephone = (String) paramMap.get("telephone");
		if (StringUtils.isBlank(telephone)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "手机号"));
			return chkRS;
		}
		if (!isValidMobileNO(StringUtils.trim(telephone))) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("非有效的手机号");
			return chkRS;
		}

		// 电子邮件非空和有效性检查
		String email = (String) paramMap.get("email");
		if (StringUtils.isBlank(email)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "电子邮件"));
			return chkRS;
		}
		if (StringUtils.trim(email).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "电子邮件", 32));
			return chkRS;
		}
		if (!isVaildEmail(StringUtils.trim(email))) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg("非有效的电子邮件");
			return chkRS;
		}

		// 所属单位非空和长度检查
		String company = (String) paramMap.get("company");
		if (StringUtils.isBlank(company)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "所属单位"));
			return chkRS;
		}
		if (StringUtils.trim(company).length() > 255) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "所属单位", 255));
			return chkRS;
		}

		// 备用联系人长度检查
		String alternateContact = (String) paramMap.get("alternateContact");
		if (StringUtils.trim(alternateContact).length() > 32) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "备用联系人", 32));
			return chkRS;
		}

		// 备用联系人电话有效性检查
		String alternateContactTel = (String) paramMap.get("alternateContactTel");
		if (StringUtils.isNotBlank(alternateContactTel)) {
			if (!isValidMobileNO(StringUtils.trim(alternateContactTel))) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg("非有效的手机号");
				return chkRS;
			}
		}

		// 自动审批非空检查
		Boolean autoApprove = (Boolean) paramMap.get("autoApprove");
		if (autoApprove == null) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "自动审批"));
		}

		// 用户组非空检查
		String groupId = (String) paramMap.get("group");
		if (StringUtils.isBlank(groupId)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户组"));
			return chkRS;
		}

		// 用户角色非空检查
		String roleCode = (String) paramMap.get("role");
		if (StringUtils.isBlank(roleCode)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户角色"));
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 注册时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkSaveLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 用户名重复性检查
		String username = (String) paramMap.get("username");
		if (userDao.countByUsername(username) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该用户名已被注册");
			return chkRS;
		}

		// 手机号重复性检查
		String telephone = (String) paramMap.get("telephone");
		if (userDao.countByTelephone(telephone) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该手机号已被注册");
			return chkRS;
		}

		// 电子邮件重复性检查
		String email = (String) paramMap.get("email");
		if (userDao.countByEmail(email) > 0) {
			chkRS.setStatus(HttpStatus.SC_CONFLICT);
			chkRS.setErrorMsg("该电子邮件已被注册");
			return chkRS;
		}
		return chkRS;
	}

	/**
	 * 编辑时非业务逻辑(非空和长度)检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateNonLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 用户名非空检查
		String username = (String) paramMap.get("username");
		if (StringUtils.isBlank(username)) {
			chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户名"));
			return chkRS;
		}

		if (paramMap.containsKey("name")) {
			// 用户姓名非空和长度检查
			String name = (String) paramMap.get("name");
			if (StringUtils.isBlank(name)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户姓名"));
				return chkRS;
			}
			if (StringUtils.trim(name).length() > 32) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "用户姓名", 32));
				return chkRS;
			}
		}

		if (paramMap.containsKey("telephone")) {
			// 手机号非空和有效性检查
			String telephone = (String) paramMap.get("telephone");
			if (StringUtils.isBlank(telephone)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "手机号"));
				return chkRS;
			}
			if (!isValidMobileNO(StringUtils.trim(telephone))) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg("非有效的手机号");
				return chkRS;
			}
		}

		if (paramMap.containsKey("email")) {
			// 电子邮件非空和有效性检查
			String email = (String) paramMap.get("email");
			if (StringUtils.isBlank(email)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "电子邮件"));
				return chkRS;
			}
			if (StringUtils.trim(email).length() > 32) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "电子邮件", 32));
				return chkRS;
			}
			if (!isVaildEmail(StringUtils.trim(email))) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg("非有效的电子邮件");
				return chkRS;
			}
		}

		if (paramMap.containsKey("company")) {
			// 所属单位非空和长度检查
			String company = (String) paramMap.get("company");
			if (StringUtils.isBlank(company)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "所属单位"));
				return chkRS;
			}
			if (StringUtils.trim(company).length() > 255) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "所属单位", 255));
				return chkRS;
			}
		}

		if (paramMap.containsKey("alternateContact")) {
			// 备用联系人长度检查
			String alternateContact = (String) paramMap.get("alternateContact");
			if (StringUtils.trim(alternateContact).length() > 32) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.MAXLENGTH_NOT_ALLOWED, "备用联系人", 32));
				return chkRS;
			}
		}

		if (paramMap.containsKey("alternateContactTel")) {
			// 备用联系人电话有效性检查
			String alternateContactTel = (String) paramMap.get("alternateContactTel");
			if (StringUtils.isNotBlank(alternateContactTel)) {
				if (!isValidMobileNO(StringUtils.trim(alternateContactTel))) {
					chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
					chkRS.setErrorMsg("非有效的手机号");
					return chkRS;
				}
			}
		}

		if (paramMap.containsKey("autoApprove")) {
			// 自动审批
			if (paramMap.get("autoApprove") == null) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "自动审批"));
				return chkRS;
			}
		}

		if (paramMap.containsKey("group")) {
			// 用户组非空检查
			String groupId = (String) paramMap.get("group");
			if (StringUtils.isBlank(groupId)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "用户组"));
				return chkRS;
			}
		}

		if (paramMap.containsKey("role")) {
			// 角色非空检查
			String roleCode = (String) paramMap.get("role");
			if (StringUtils.isBlank(roleCode)) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NULL_NOT_ALLOWED, "角色"));
				return chkRS;
			}
		}

		return chkRS;
	}

	/**
	 * 编辑时业务逻辑检查
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return CheckResult检查结果对象
	 */
	private CheckResult checkUpdateLogic(Map<String, Object> paramMap) {
		CheckResult chkRS = new CheckResult();

		// 用户名重复性检查
		String username = (String) paramMap.get("username");
		UserEntity user = userDao.getUser(username, null);
		if (user == null) {
			chkRS.setStatus(HttpStatus.SC_GONE);
			chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "用户"));
			return chkRS;
		}

		if (!user.getAuthType().equals("native")) {
			if (!user.getName().equals(paramMap.get("name")) || !user.getTelephone().equals(paramMap.get("telephone"))
					|| !user.getEmail().equals((String) paramMap.get("email"))
					|| !user.getCompany().equals((String) paramMap.get("company"))) {
				chkRS.setStatus(HttpStatus.SC_BAD_REQUEST);
				chkRS.setErrorMsg("非平台认证用户无法修改用户姓名、手机号、电子邮件和所属单位");
				return chkRS;
			}
		}

		// 手机号重复性检查
		if (paramMap.containsKey("telephone")) {
			String telephone = (String) paramMap.get("telephone");
			if (!user.getTelephone().equals(telephone)) {
				if (userDao.countByTelephone(telephone) > 0) {
					chkRS.setStatus(HttpStatus.SC_CONFLICT);
					chkRS.setErrorMsg("该手机号已被注册");
					return chkRS;
				}
			}
		}

		// 电子邮件重复性检查
		if (paramMap.containsKey("email")) {
			String email = (String) paramMap.get("email");
			if (!user.getEmail().equals(email)) {
				if (userDao.countByEmail(email) > 0) {
					chkRS.setStatus(HttpStatus.SC_CONFLICT);
					chkRS.setErrorMsg("该电子邮件已被注册");
					return chkRS;
				}
			}
		}

		// 用户角色存在性检查
		if (paramMap.containsKey("roleCode")) {
			String roleCode = (String) paramMap.get("roleCode");
			if (userRoleDao.countByCode(roleCode) == 0) {
				chkRS.setStatus(HttpStatus.SC_GONE);
				chkRS.setErrorMsg(MessageFormat.format(Messages.NOT_EXIST, "用户角色"));
				return chkRS;
			}
		}
		return chkRS;
	}

}
